package pe.edu.upla.lamerced.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.DispatchAction;
import org.springframework.beans.factory.annotation.Autowired;

import pe.edu.upla.lamerced.bean.Categoria;
import pe.edu.upla.lamerced.service.CategoriaService;
import pe.edu.upla.lamerced.utiles.Utiles;

public class CategoriasAction extends DispatchAction {
	
	private static final Logger logger = Logger.getLogger(CategoriasAction.class);
	
	@Autowired
	CategoriaService categoriaService;
	
	public ActionForward lista(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)	throws Exception {
		try {
			request.setAttribute("lstCategorias", categoriaService.listarCategorias());
           logger.debug("lista de categorias");
		} catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msgError", e.getMessage());
            return mapping.findForward("home");
		}
		return mapping.findForward("lista");
	}
	
	
	public ActionForward preNuevo(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)	throws Exception {
		try {
           logger.debug("lista de categorias");
		} catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msgError", e.getMessage());
            return mapping.findForward("home");
		}
		return mapping.findForward("nueva");
	}
	
	public ActionForward nuevo(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)	throws Exception {
		try {
           logger.debug("lista de categorias");
           DynaActionForm myform = (DynaActionForm)form;
           Categoria categoria = new Categoria();
           categoria.setNombre(myform.getString("nombre"));
           if(!Utiles.nullToBlank(myform.getString("idcategoria")).equals("")){
        	   categoria.setIdcategoria(Integer.parseInt(myform.getString("idcategoria")));
           }
           categoriaService.registrarCategoria(categoria);
           request.setAttribute("mensaje", "categoria registrada");
		} catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msgError", e.getMessage());
            return mapping.findForward("nueva");
		}
		return lista(mapping, form, request, response);
	}
	
	public ActionForward cancelNuevo(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)	throws Exception {
		try {
           logger.debug("cancela de categorias");
		} catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msgError", e.getMessage());
            return mapping.findForward("home");
		}
		return lista(mapping, form, request, response);
	}
	
	public ActionForward preEditar(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)	throws Exception {
		try {
           logger.debug("lista de categorias");
           String cat = request.getParameter("cod");
           Categoria categ =  categoriaService.obtenercategoria(Integer.parseInt(cat));
           DynaActionForm mform = (DynaActionForm)form;
           mform.set("nombre", categ.getNombre());
           mform.set("idcategoria", categ.getIdcategoria().toString());
		} catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msgError", e.getMessage());
            return mapping.findForward("home");
		}
		return mapping.findForward("nueva");
	}
	
	public ActionForward detalle(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)	throws Exception {
		try {
           logger.debug("lista de categorias");
           String cat = request.getParameter("cod");
           request.setAttribute("dcat", categoriaService.obtenercategoria(Integer.parseInt(cat)));
		} catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msgError", e.getMessage());
            return mapping.findForward("home");
		}
		return mapping.findForward("detalle");
	}
}
